<template>
	<view class="mainbgc">
		<header-top :messageInfo="{typeFlag:true,message:titletext,liner:false}">
			<view class="rightmsg" slot="rightmsg">
				<view class="rightmsg_content flexright" @click="submit">
					<view class="nostrongtext">保存</view>
				</view>
			</view>
		</header-top>
		<view class="mainpadding">
			<view class="strongtext">
				<text class="titletext textliner">基本信息</text>
			</view>
		</view>
		<view class="mainpadding ffffff">
			<view class="inputbox">
				<view class="flexleft flex1">
					<view class="strongtext margin_right">仓库名称</view>
					<view class="bitian">*</view>
				</view>
				<view class="itembox">
					<input type="text" placeholder="请输入仓库名称" v-model="name">
				</view>
			</view>
			<view class="inputbox margin_top">
				<view class="flexleft flex1">
					<view class="strongtext margin_right">仓库类型</view>
					<view class="bitian">*</view>
				</view>
				<view class="itembox flexbetween" @click="ckLeiXingShow = true">
					<input type="text" placeholder="请选择仓库类型" v-model="ckLeiXingText" disabled="">
					<view class="righticon2"></view>
				</view>
			</view>
			<view class="inputbox margin_top">
				<view class="flexleft flex1">
					<view class="strongtext margin_right">仓库片区</view>
					<view class="bitian">*</view>
				</view>
				<view class="itembox flexbetween" @click="ckPianQuShow = true">
					<input type="text" placeholder="请选择仓库片区" v-model="ckPianQuText" disabled="">
					<view class="righticon2"></view>
				</view>
			</view>
			<view class="inputbox margin_top">
				<view class="flexleft flex1">
					<view class="strongtext margin_right">部门</view>
					<view class="bitian">*</view>
				</view>
				<view class="itembox flexbetween" @click="ckBuMenShow = true">
					<input type="text" placeholder="请选择部门" v-model="ckBuMenText" disabled="">
					<view class="righticon2"></view>
				</view>
			</view>
			<uni-data-picker :localdata="items" placeholder="请选择住址" popup-title="请选择地区" v-model="addressinfo.county"
				@change="bindPickerChange">
				<view class="inputbox margin_top">
					<view class="flexleft flex1">
						<view class="strongtext margin_right">省市区选择</view>
						<view class="bitian">*</view>
					</view>
					<view class="itembox flexbetween">
						<text class="word12" v-if="!area.length">请选择住址</text>
						<text class="word13" v-else>{{ area[0] }}/{{ area[1] }}/{{ area[2] }}</text>
						<view class="righticon2"></view>
					</view>
				</view>
			</uni-data-picker>
			<view class="inputbox margin_top">
				<view class="flexleft flex1">
					<view class="strongtext margin_right">详细地址</view>
					<view class="bitian">*</view>
				</view>
				<view class="itembox flexbetween" @click="getLocation">
					<input type="text" placeholder="请选择详细地址" disabled="" v-model="address">
					<view class="addressicon2"></view>
				</view>
			</view>
			<view class="inputbox margin_top">
				<view class="flexleft flex1">
					<view class="strongtext margin_right">负责人</view>
					<view class="bitian">*</view>
				</view>
				<view class="itembox flexbetween" @click="selectFuZeRen()">
					<input type="text" placeholder="请选择负责人" v-model="ckFuZeRText" disabled="">
					<view class="righticon2"></view>
				</view>
			</view>
			<view class="inputbox margin_top">
				<view class="flexleft flex1">
					<view class="strongtext margin_right">负责区域</view>
				</view>
				<view class="itembox flexbetween">
					<input type="text" placeholder="输入负责区域" v-model="fz_area">
				</view>
			</view>
		</view>
		<selectCKPianQu :message="{ckPianQuShow:ckPianQuShow}" @ckPianQuSure="ckPianQuSure"></selectCKPianQu>
		<!-- 仓库片区 -->
		<selectCKLeiXing :message="{ckLeiXingShow:ckLeiXingShow}" @ckLeiXingSure="ckLeiXingSure"></selectCKLeiXing>
		<!-- 仓库类型 -->
		<selectCKBuMen :message="{ckBuMenShow:ckBuMenShow}" @ckBuMenSure="ckBuMenSure"></selectCKBuMen><!-- 仓库部门 -->
	</view>
</template>
<script>
	const httpRequest = require("@/common/httpRequest.js")
	import citys from "@/static/city/city.js"
	export default {
		data() {
			return {
				// 仓库类型
				ckLeiXingShow: false,
				ckLeiXingText: "",
				warehouse_type_id: "", //仓库类型id
				// 仓库片区
				ckPianQuShow: false,
				ckPianQuText: "",
				staff_area_id: "", //仓库片区id
				// 仓库部门
				ckBuMenShow: false,
				ckBuMenText: "",
				staff_bm_id: "", //仓库部门id
				// 仓库负责人
				ckFuZeRText: "",
				staff_id: "", //仓库负责人id
				// 省市区选择
				addressinfo: {
					province: "",
					city: "",
					county: ""
				},
				// #ifdef APP-PLUS
				address: "", //详细地址
				// #endif
				// #ifdef MP-WEIXIN || H5
				address: "详细地址", //详细地址
				// #endif
				fz_area: "", //负责区域
				longitude: "109.67307", //经度
				latitude: "35.17466", //纬度
				name: "", //仓库名称
				items: [],
				area: [], //收获地址
				listid: "",
				titletext: "添加仓库档案"
			}
		},
		onLoad(options) {
			this.listid = options.id
			this.items = JSON.parse(JSON.stringify(citys.data))
			if (this.listid) {
				this.titletext = "编辑仓库档案"
				this.getdetail()
			}
		},
		onShow() {
			uni.$off('ygSelectFn')
			uni.$on('ygSelectFn', data => {
				this.staff_id = data.id
				this.ckFuZeRText = data.name
			})
		},
		methods: {
			ckBuMenSure(e) { //仓库部门
				if (e) {
					this.ckBuMenText = e.value[0].name
					this.staff_bm_id = e.value[0].id
				}
				this.ckBuMenShow = false
			},
			ckLeiXingSure(e) { //仓库类型
				if (e) {
					this.ckLeiXingText = e.value[0].name
					this.warehouse_type_id = e.value[0].id
				}
				this.ckLeiXingShow = false
			},
			ckPianQuSure(e) { //仓库片区
				if (e) {
					this.ckPianQuText = e.value[0].name
					this.staff_area_id = e.value[0].id
				}
				this.ckPianQuShow = false
			},
			// 选择负责人（选择员工）
			selectFuZeRen() {
				uni.navigateTo({
					url: "/pages/workbench/jiben/yuangongda?type=1"
				})
			},
			getLocation() {
				let _this = this
				uni.chooseLocation({
					success: function(res) {
						// _this.address = res.address.replace(/.+?(省|市|自治区|自治州|县|区)/g, '');
						_this.longitude = res.longitude
						_this.latitude = res.latitude
						_this.address = res.address
					}
				})
			},
			// 获取详情数据
			getdetail() {
				httpRequest.request('/dhtapi/Warehouse/detail', 'POST', {
					id: this.listid
				}, false, false, true).then(res => {
					let data = res.data;
					this.staff_area_id = data.staff_area_id
					this.warehouse_type_id = data.warehouse_type_id
					this.staff_bm_id = data.staff_bm_id
					this.addressinfo.province = data.province_id
					this.addressinfo.city = data.city_id
					this.addressinfo.county = data.area_id
					this.address = data.address
					this.staff_id = data.staff_id
					this.fz_area = data.fz_area
					this.longitude = data.longitude
					this.latitude = data.latitude
					this.name = data.name
					let arr = data.cityinfo.split("/")
					this.area = [arr[0], arr[1], arr[2]]
					this.ckFuZeRText = data.staff.username
					this.ckBuMenText = data.staffbm.name
					this.ckPianQuText = data.staffarea.name
					this.ckLeiXingText = data.warehousetype.name
				})
			},
			bindPickerChange(val) {
				let area = val.detail.value
				this.area = [area[0].text, area[1].text, area[2].text];
				this.addressinfo.province = area[0].value;
				this.addressinfo.city = area[1].value;
				this.addressinfo.county = area[2].value;
			},
			submit() {
				let _this = this;
				if (_this.name == '') {
					httpRequest.toast('请输入仓库名称!')
					return false;
				}
				if (_this.warehouse_type_id == '') {
					httpRequest.toast('请选择仓库类型!')
					return false;
				}
				if (_this.staff_area_id == '') {
					httpRequest.toast('请选择仓库片区!')
					return false;
				}
				if (_this.staff_bm_id == '') {
					httpRequest.toast('请选择部门!')
					return false;
				}
				if (_this.addressinfo.province == '') {
					httpRequest.toast('请选择省市区地址!')
					return false;
				}
				if (_this.address == '') {
					httpRequest.toast('请选择详细地址!')
					return false;
				}
				if (_this.staff_id == '') {
					httpRequest.toast('请选择负责人!')
					return false;
				}
				let data = {
					staff_area_id: this.staff_area_id,
					warehouse_type_id: this.warehouse_type_id,
					staff_bm_id: this.staff_bm_id,
					province_id: this.addressinfo.province,
					city_id: this.addressinfo.city,
					area_id: this.addressinfo.county,
					address: this.address,
					staff_id: this.staff_id,
					fz_area: this.fz_area,
					longitude: this.longitude,
					latitude: this.latitude,
					name: this.name,
					id: this.listid
				}
				let url = ""
				if (!this.listid) {
					// 新增
					delete data.id
					url = '/dhtapi/Warehouse/addWarehouse'
				} else {
					// 编辑
					url = '/dhtapi/Warehouse/editWarehouse'
				}
				httpRequest.request(url, 'GET', data).then(res => {
					if (res.code == 1) {
						httpRequest.toast(res.msg)
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	.flex1 {
		flex: 3;
	}

	.inputbox {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
	}

	.itembox {
		flex: 7;
		width: 100%;
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid rgba(153, 153, 153, 0.06);
	}

	.word12 {
		width: 300rpx;
	}
</style>